package com.thinktank.controller;

import java.io.IOException;
import java.sql.Connection;
import java.util.List;

import javax.servlet.*;
import javax.servlet.http.*;

import com.thinktank.dao.*;
import com.thinktank.vo.*;

import static com.thinktank.util.DbUtil.*;

public class AlbumController implements Controller {
	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws IOException, ServletException {
		// 세션 확인 및 쿠키 로그인
		HttpSession session = request.getSession();
		if (session.getAttribute("member") == null) {
			Cookie[] cookies = request.getCookies();
			if (cookies != null) {
				String no = null;
				for (int i = 0; i < cookies.length; i++)
					if (cookies[i].getName().equals("no"))
						no = cookies[i].getValue();
				if (no != null) {
					System.out.println("쿠키로그인");
					Connection con = getConnection();
					MemberDao dao = new MemberDao(con);
					Member member = dao.cookieLogin(no);
					close(con);
					if (member != null)
						session.setAttribute("member", member);
				}
			}
		}
		// 방문 페이지 세션에 저장
		String url = null;
		if (request.getParameter("url") != null)
			url = request.getParameter("url");
		if (url == null) {
			Member member = (Member) session.getAttribute("member");
			url = member.getmPage();
		}

		System.out.println("Album Controller!");

		// 경로 설정
		ServletContext sc = request.getServletContext();
		String path = sc.getRealPath("\\upload");

		Connection con = getConnection();
		AlbumDao dao = new AlbumDao(con);
		List<Album> list = dao.getAllAlbums(url);
		close(con);
		request.setAttribute("list", list);
		request.setAttribute("path", path);

		return "album.jsp";
	}

}
